/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TimersView.java
 *
 * Created on 30/09/2010, 15:56:29
 */

package atualiza;

import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import model.atualizaDao;

/**
 *
 * @author Administrador
 */
public class TimersView extends javax.swing.JInternalFrame {

    /** Creates new form TimersView */
    public TimersView() {
        initComponents();

        manipulaConexao.preenchetableMy(tb_timers, "select a.id_timers,b.nome from tb_timers a inner join processo b on a.id_processo = b.idprocesso");

        tb_timers.getSelectionModel().addListSelectionListener(selectionListener);
        tb_timers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //pane.add(desc);

        //Cria a reorganização da tabela através do clique na coluna
        tb_timers.setAutoCreateRowSorter(true);
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tb_timers.getModel());
        tb_timers.setRowSorter(sorter);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tb_timers = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jbt_novo = new javax.swing.JButton();
        jbt_salvar = new javax.swing.JButton();
        jbt_cancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jtf_processo = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jcb_periodo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jft_int_inicio = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        jft_int_final = new javax.swing.JFormattedTextField();
        jcb_intervalo = new javax.swing.JComboBox();
        jck_domingo = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();
        jFormattedTextField3 = new javax.swing.JFormattedTextField();
        jck_suspender = new javax.swing.JCheckBox();

        setTitle("Timers");

        tb_timers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tb_timers);

        jbt_novo.setText("Novo");
        jbt_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_novoActionPerformed(evt);
            }
        });
        jPanel1.add(jbt_novo);

        jbt_salvar.setText("Salvar");
        jbt_salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_salvarActionPerformed(evt);
            }
        });
        jPanel1.add(jbt_salvar);

        jbt_cancelar.setText("Cancelar");
        jbt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_cancelarActionPerformed(evt);
            }
        });
        jPanel1.add(jbt_cancelar);

        jLabel1.setText("Processo: ");

        jtf_processo.setEditable(false);

        jButton3.setText("Pesquisar Processos");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText("Periodicidade:");

        jcb_periodo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Diário", "Semanal", "Mensal", "Intervalo", "Dia específico" }));
        jcb_periodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcb_periodoActionPerformed(evt);
            }
        });

        jLabel3.setText("Intervalo:");

        jft_int_inicio.setEditable(false);
        jft_int_inicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jft_int_inicio.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jft_int_inicio.setText("00");

        jLabel4.setText("à");

        jft_int_final.setEditable(false);
        jft_int_final.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jft_int_final.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jft_int_final.setText("00");

        jcb_intervalo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dia(s)", "Mes(es)", "Ano(s)" }));

        jck_domingo.setText("Gera Sábado / Domingo");

        jLabel5.setText("Horário:");

        jFormattedTextField3.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT))));
        jFormattedTextField3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jFormattedTextField3.setText("07:00");

        jck_suspender.setText("Supender");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jcb_periodo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jtf_processo, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton3))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jft_int_inicio, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jft_int_final, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jFormattedTextField3, 0, 0, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jck_domingo)
                                    .addComponent(jck_suspender)
                                    .addComponent(jcb_intervalo, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(26, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton3)
                    .addComponent(jtf_processo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcb_periodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcb_intervalo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jft_int_final, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jft_int_inicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jck_suspender)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jck_domingo)))
                .addGap(9, 9, 9)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-388)/2, (screenSize.height-375)/2, 388, 375);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int id = 0;
        pesquisa pesquisa = new pesquisa(null, true, "Select idprocesso,nome from processo", id);
        pesquisa.setVisible(true);
        jtf_processo.setText(String.valueOf(pesquisa.getId()));
        pesquisa.hide();
    }//GEN-LAST:event_jButton3ActionPerformed


    private ListSelectionListener selectionListener = new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e)
        {
             try{

            int linha = tb_timers.getSelectedRow();
            if(linha == -1)
            {
                //JOptionPane.showMessageDialog(null, "Não há tarefa selecionada, verifique!");
                return;
            }
            int idtarefa =  Integer.valueOf(String.valueOf(tb_timers.getValueAt(linha,0)));
            if(idtarefa != 0)
            {
                int periodo = 0;
                int intervalo = 0;
                int intin = 0, intfin = 0 ;
                String proc = "";
                String horario = "";
                boolean suspender = false;
                boolean domingo = false;
                new atualizaDao().CarregaTimer(idtarefa,proc,periodo,intin,intfin, intervalo, horario,suspender,domingo);
                JOptionPane.showMessageDialog(null, proc);
                jtf_processo.setText(proc);
                jft_int_inicio.setText(String.valueOf(intin));
                jft_int_final.setText(String.valueOf(intfin));



            }
            //String desc = String.valueOf(jtbtarefas.getValueAt(linha, 2));
            //jtbtarefas.setToolTipText(desc);
        }catch(Exception exc)
        {
            JOptionPane.showMessageDialog(null, exc.getMessage(),"Erro desconhecido",0);
        }

        }


    };



    private void jcb_periodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcb_periodoActionPerformed

        int index = jcb_periodo.getSelectedIndex();
        BloqueiaItens(index);

    }//GEN-LAST:event_jcb_periodoActionPerformed

    private void jbt_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_novoActionPerformed
        novo = true;
        jtf_processo.setText("");
        jft_int_final.setText("00");
        jft_int_inicio.setText("00");
        jcb_periodo.setSelectedIndex(0);
        jcb_intervalo.setSelectedIndex(0);
        jck_domingo.setSelected(false);
        jck_suspender.setSelected(false);
        jbt_novo.setEnabled(false);

       
    }//GEN-LAST:event_jbt_novoActionPerformed

    private void jbt_salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_salvarActionPerformed

       try
       {
           int id = Integer.parseInt(jtf_processo.getText());
           int id_periodo = jcb_periodo.getSelectedIndex();
           if(id_periodo > 2)
           {
           int intervalo_inicial = Integer.parseInt(jft_int_inicio.getText());
           int intervalo_final = 0;
           int id_intervalo = 0;
           if(id_periodo == 3)
           {
               intervalo_final = Integer.parseInt(jft_int_final.getText());
               id_intervalo = jcb_intervalo.getSelectedIndex();
               }                      
           id_intervalo = jcb_intervalo.getSelectedIndex();
           String hora = jFormattedTextField3.getText();
           boolean domingo = jck_domingo.isSelected();
           boolean suspender = jck_suspender.isSelected();
           new atualizaDao().gravaTimers(id, id_periodo,intervalo_inicial,intervalo_final,id_intervalo, hora, domingo,suspender);
           }else
           {
               String hora = jFormattedTextField3.getText();
               boolean domingo = jck_domingo.isSelected();
               boolean suspender = jck_suspender.isSelected();
               new atualizaDao().gravaTimers(id, id_periodo, 0, 0, 0, hora, domingo, suspender);
           }
       }catch(NumberFormatException exc)
       {
           JOptionPane.showMessageDialog(null,exc.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
           return;
       }


    }//GEN-LAST:event_jbt_salvarActionPerformed

    private void jbt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_cancelarActionPerformed
        
    }//GEN-LAST:event_jbt_cancelarActionPerformed

    private void BloqueiaItens(int index)
    {
        switch(index)
        {
            case 0:
                jft_int_inicio.setEditable(false);
                jft_int_final.setEditable(false);
                jcb_periodo.setEditable(false);
                break;
            case 1:
                jft_int_inicio.setEditable(false);
                jft_int_final.setEditable(false);
                jcb_periodo.setEditable(false);
                break;
            case 2:
                jft_int_inicio.setEditable(false);
                jft_int_final.setEditable(false);
                jcb_periodo.setEditable(false);
                break;
            case 3:
                jft_int_inicio.setEditable(true);
                jft_int_final.setEditable(true);
                jcb_periodo.setEditable(true);
                break;
            case 4:
                jft_int_inicio.setEditable(true);
                jft_int_final.setEditable(false);
                jcb_periodo.setEditable(false);
                break;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JFormattedTextField jFormattedTextField3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbt_cancelar;
    private javax.swing.JButton jbt_novo;
    private javax.swing.JButton jbt_salvar;
    private javax.swing.JComboBox jcb_intervalo;
    private javax.swing.JComboBox jcb_periodo;
    private javax.swing.JCheckBox jck_domingo;
    private javax.swing.JCheckBox jck_suspender;
    private javax.swing.JFormattedTextField jft_int_final;
    private javax.swing.JFormattedTextField jft_int_inicio;
    private javax.swing.JTextField jtf_processo;
    private javax.swing.JTable tb_timers;
    // End of variables declaration//GEN-END:variables
    private boolean novo = true;
}
